package com.regan.erp.aop;

import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.ObjectUtil;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.log.Log;
import com.regan.erp.exception.MyBaselogicException;
import com.regan.erp.model.LogExpression;
import com.regan.erp.model.ResultPoJo;
import com.regan.erp.model.User;
import com.regan.erp.util.Constants;

import java.io.FileNotFoundException;
import java.sql.SQLException;


/**
 * 异常捕获处理
 *
 * @author zhangby
 * @date 2017/11/2 17:23
 */
public class MyExceptionInterceptor implements Interceptor {
    static Log logger = Log.getLog(MyExceptionInterceptor.class);

    public void intercept(Invocation inv) {
        try {
            inv.invoke();
        } catch (MyBaselogicException e) {
            String msg = Constants.error_msg(e.getNum());
            if (ObjectUtil.isNotNull(e.getMsg())) {
                msg = StrFormatter.format(msg, e.getMsg());
            }
            //返回错误信息
            if (inv.getMethodName().indexOf("to") > -1) {
                inv.getController().setAttr("msg", msg);
                //页面统一跳转错误页面
                inv.getController().render("/html/other/error.html");
            } else {
                logger.info(Constants.getExceptionMsg(e));
                ResultPoJo poJo = ResultPoJo.create()
                        .code(Constants.error_code(e.getNum()))
                        .msg(msg);
                inv.getController().renderJson(poJo);
            }
            logger.error(e.getNum() + " : " + msg);
            System.err.println(e.getNum() + " : " + msg);
            //捕获自定义异常
            e.printStackTrace();
        } catch (Exception e) {
            //捕获系统异常
            e.printStackTrace();
            //判断异常信息
            String problem = "系统出现异常";
            if (e instanceof NullPointerException)
                problem = "NullPointerException 空指针异常！";
            else if (e instanceof ClassNotFoundException)
                problem = "ClassNotFoundException 指定的类不存在！";
            else if (e instanceof ClassCastException)
                problem = "ClassCastException 类型转换异常！";
            else if (e instanceof ArrayIndexOutOfBoundsException)
                problem = "ArrayIndexOutOfBoundsException 数据下标越界异常！";
            else if (e instanceof FileNotFoundException)
                problem = "FileNotFoundException 文件未找到异常！";
            else if (e instanceof NumberFormatException)
                problem = "NumberFormatException 数字转换异常！";
            else if (e instanceof SQLException)
                problem = "SQLException 数据库操作异常！";
            else if (e instanceof NoSuchMethodException)
                problem = "NoSuchMethodException 方法未找到异常！";
            User user = (User) inv.getController().getSession().getAttribute("user");
            //添加异常日志
            LogExpression expression = new LogExpression()
                    .setController(inv.getController().getClass().getName())
                    .setMethod(inv.getMethodName())
                    .setUrl(inv.getController().getRequest().getRequestURL().toString())
                    .setParam(inv.getController().getRequest().getQueryString())
                    .setProblem(problem)
                    .setExpression(Constants.getExceptionMsg(e));
            if (ObjectUtil.isNotNull(user)) {
                expression.setUserId(user.getId());
            }
            expression.save();

            //返回错误信息
            if (inv.getMethodName().indexOf("to") > -1) {
                //页面统一跳转错误页面
                inv.getController().render("/html/other/error.html");
            } else {
                ResultPoJo msg = ResultPoJo.create()
                        .code(Constants.ERROR)
                        .msg(problem);
                inv.getController().renderJson(msg);
            }
        }
    }

}